﻿using System;
using Framework.MVC;
using Log.Model;
using Identity.Model;
using Identity.IService.Entry;
using Framework.MVVM.Model;
using Identity.IService;
using Log.IService;
using Common.IService;
using Identity.Service;
using Log.Service;
using Common.Service;


namespace Recharge.Models
{
    [ServiceAttribute(typeof(IIdentityService), typeof(IdentityService), "Instance")]
    [ServiceAttribute(typeof(ILogService), typeof(LogService), "Instance")]
    [ServiceAttribute(typeof(ICodeService), typeof(CodeService), "Instance")]
    public class ValidateIdentityModel : AsyncModelBase, IWriteLog, IValidateIdentity
    {
        #region Field

        private const string m_empty_identity = "rcgrview0";
        private const string m_error_identity = "rcgrview1";
        private const string m_complete_identity = "rcgrview2";

        private const string m_user_resource = "u000";

        private const string m_entry = "entry";

        #endregion Field

        #region AsyncModelBase

        protected override void OnInit(Type type)
        {
            OnBeginRequest();
            this.ValidateIdentity(m_user_resource, m_empty_identity, m_error_identity, m_complete_identity);
        }

        #endregion AsyncModelBase

        #region Write Log

        public void OnWriteLogComplete(string eventCode)
        {
            OnEndRequest();
        }

        public void OnWriteLogError(string eventCode)
        {
            OnEndRequest();
        }

        #endregion Write Log

        #region IValidateIdentity

        public string Source { get; set; }

        public string IdentityCode { get; set; }

        public void OnValidateIdentityComplete(string key, IdentityEntry entry)
        {
            OnComplete(m_entry,entry);
            this.Log(key, entry);
        }

        public void OnValidateIdentityError(string key)
        {
            this.Log(key, new IdentityEntry { code = IdentityCode, source = Source, resource = m_user_resource });
        }

        #endregion IValidateIdentity
    }
}